<?php
	// Search and modify illegal portions
	$_POST = str_replace("'","''",$_POST);
	$_POST = str_replace("\\","\\\\",$_POST);
	
	// Search for unmodifable errors
	$clean = true;
	$foundMajNError = false;
	$foundMajPError = false;
	$foundMinNError = false;
	$foundMinSError = false;
	$error = "<div align=\"center\">";
	$error = $error . "The information you submitted have the following errors:<br/><div style='color:#F00'>";
	
	// Check for empty template name
	if(strlen($_POST['template'])==0){
		$error = $error . "Template name is required.<br/>";
		$clean = false;
	}
	
	// Check for an empty major name and percentage
	for($i=0;;$i++){
		// When there are no more entries, end check
		if(!array_key_exists('majName'.$i,$_POST) || ($foundMajNError && $foundMajPError && $foundMinNError && $foundMinSError))
			break;
		
		// Check for major name
		if(strlen($_POST['majName'.$i])==0 && !$foundMajNError){
			$error = $error . "At least one major requirement name is unspecified.<br/>";
			$foundMajNError = true;
			$clean = false;
		}
		
		// Check for major percentage
		if(strlen($_POST['majPer'.$i])==0 && !$foundMajPError){
			$error = $error . "At least one major requirement percentage is unspecified.<br/>";
			$foundMajPError = true;
			$clean = false;
		}
		
		// Check all associated minor requirements as well
		for($j=0;;$j++){
			// When there are no more entries, end check
			if(!array_key_exists('minName'.$i.$j,$_POST) || ($foundMinNError && $foundMinSError))
				break;
				
			// Check for minor name
			if(strlen($_POST['minName'.$i.$j])==0 && !$foundMinNError){
				$error = $error . "At least one minor requirement name is unspecified.<br/>";
				$foundMinNError = true;
				$clean = false;
			}
			
			// Check for minor max raw score
			if(strlen($_POST['minSco'.$i.$j])==0  && !$foundMinSError){
				$error = $error . "At least one minor max raw score is unspecified.<br/>";
				$foundMinSError = true;
				$clean = false;
			}
		}
	}
	
	// Check if all percentages and max raw scores are numbers
	for($i=0;!$foundMajPError || !$foundMinSError;$i++){
		// When there are no more entries, end check
		if(!array_key_exists('majName'.$i,$_POST))
			break;
		
		// Check percentages
		if(!ctype_digit($_POST['majPer'.$i]) && !$foundMajPError && $_POST['majPer'.$i]!="~NULL"){
			$error = $error . "At least one percentage is not a number.<br/>";
			$foundMajPError = true;
			$clean = false;
		}
		
		for($j=0;!$foundMinSError;$j++){
			// When there are no more entries, end check
			if(!array_key_exists('minName'.$i.$j,$_POST))
				break;
			
			// Check max raw scores
			if(!ctype_digit($_POST['minSco'.$i.$j]) && !$foundMinSError && $_POST['minSco'.$i.$j]!="~NULL"){
				$error = $error . "At least one max raw score is not a number.<br/>";
				$foundMinSError = true;
				$clean = false;
			}
		}
	}
	
	// Check for the total of percentages
	$percentage = 0;
	for($i=0;!$foundMajPError;$i++){
		// When there are no more entries, end check
		if(!array_key_exists('majName'.$i,$_POST))
			break;
		
		// Accumulate all percentages
		if($_POST['majPer'.$i]!="~NULL")
			$percentage = $percentage + intval($_POST['majPer'.$i]);
	}
	if($percentage != 100 && !$foundMajPError){
		$error = $error . "Total of percentages is not equal to 100.<br/>";
		$clean = false;
	}
	$error = $error . "</div>Please check the information you have submitted.<br/>";
?>